package com.vsen.lpr.service;

import com.vsen.lpr.common.exception.BusinessException;
import com.vsen.lpr.common.model.Response;
import com.vsen.lpr.common.utils.BeanCopyUtil;
import com.vsen.lpr.entity.ProductInfo;
import com.vsen.lpr.entity.ProductLabel;
import com.vsen.lpr.entity.ShoppingCart;
import com.vsen.lpr.entity.ext.ShoppingCartInfoDo;
import com.vsen.lpr.mapper.ProductInfoMapper;
import com.vsen.lpr.mapper.ProductLabelMapper;
import com.vsen.lpr.mapper.ShoppingCartMapper;
import com.vsen.lpr.pojo.dto.ShoppingCartDto;
import com.vsen.lpr.pojo.dto.shoppingListDto;
import com.vsen.lpr.pojo.vo.ShoppingCartVo;
import com.vsen.lpr.pojo.vo.ShoppingProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class ShoppingCardService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private ProductLabelMapper productLabelMapper;

    public Response addShoppingCart(ShoppingCartDto dto) {
        checkParam(dto);
        if (dto.getShopId() == null) {
            throw new BusinessException("10001", "参数有误");
        }
        int count = 0;
        //没有标签的商品
        if (dto.getLabelId() == null) {
            count = shoppingCartMapper.selectByProductId(dto.getProductId(), dto.getUserId());
            if (count == 0) {
                insertShoppingCart(dto);
            } else {
                shoppingCartMapper.addShoppingCartNoLabel(dto.getProductId(), dto.getUserId());
            }
        } else {
            count = shoppingCartMapper.selectByProductIdAndLabel(dto.getProductId(), dto.getLabelId(), dto.getUserId());
            if (count == 0) {
                insertShoppingCart(dto);
            } else {
                shoppingCartMapper.addShoppingCart(dto.getProductId(), dto.getLabelId(), dto.getUserId());
            }
        }
        return Response.success();
    }

    private void insertShoppingCart(ShoppingCartDto dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanCopyUtil.copyFromSrcToDest(dto, shoppingCart);
        shoppingCart.setNum(1);
        shoppingCartMapper.insert(shoppingCart);
    }

    private void checkParam(ShoppingCartDto dto) {
        if (dto.getUserId() == null || dto.getProductId() == null) {
            throw new BusinessException("10001", "参数有误");
        }

        ProductInfo productInfo = productInfoMapper.selectByProductId(dto.getProductId());
        if (productInfo == null) {
            throw new BusinessException("20001", "商品不存在");
        }
        List<ProductLabel> labels = productLabelMapper.selectByProductId(dto.getProductId());
        if (labels.size() > 0) {
            Set<Integer> labelSet = new HashSet<>();
            for (ProductLabel p : labels) {
                labelSet.add(p.getId());
            }
            if (dto.getLabelId() == null) {
                throw new BusinessException("20003", "标签类商品请选择标签");
            }
            if (!labelSet.contains(dto.getLabelId())) {
                throw new BusinessException("20002", "商品标签不存在");
            }
        } else {
            if (dto.getLabelId() != null) {
                throw new BusinessException("20004", "该商品没有标签");
            }
        }

    }

    public Response reduceShoppingCart(ShoppingCartDto dto) {
        checkParam(dto);
        if(dto.getShoppingCartPage()==null){
            throw new BusinessException("10001", "参数有误");
        }
        int count = 0;
        //带标签的商品
        if (dto.getLabelId() != null) {
            count = shoppingCartMapper.selectByProductIdAndLabel(dto.getProductId(), dto.getLabelId(), dto.getUserId());
            if (count == 0) {
                return Response.success();
            }
            int nums = shoppingCartMapper.selectByProductId(dto.getProductId(), dto.getUserId());
            if (nums > 1 && dto.getShoppingCartPage() == 0) {
                throw new BusinessException("20006", "不同规格的商品，请到购物车中删除哦～");
            }
            int totalNum = shoppingCartMapper.selectNumByProductId(dto.getProductId(), dto.getLabelId(), dto.getUserId());
            if (totalNum == 1) {
                shoppingCartMapper.deleteShoppingCart(dto.getProductId(), dto.getLabelId(), dto.getUserId());
            } else {
                shoppingCartMapper.reduceShoppingCart(dto.getProductId(), dto.getLabelId(), dto.getUserId());
            }
        } else {
            int totalNum = shoppingCartMapper.selectNumByProductIdNoLabel(dto.getProductId(), dto.getUserId());
            if (totalNum == 1) {
                shoppingCartMapper.deleteShoppingCartNoLabel(dto.getProductId(), dto.getUserId());
            } else {
                shoppingCartMapper.reduceShoppingCartNoLabel(dto.getProductId(), dto.getUserId());
            }
        }
        return Response.success();
    }

    /**
     * 购物车列表
     *
     * @param dto
     * @return
     */
    public Response<ShoppingCartVo> shoppingList(shoppingListDto dto) {
        if (dto.getShopId() == null && dto.getUserId() == null) {
            throw new BusinessException("10001", "参数有误");
        }
        ShoppingCartVo shoppingCartVo = new ShoppingCartVo();
        List<ShoppingCartInfoDo> shoppingCarts = shoppingCartMapper.selectCartInfoByUserId(dto.getUserId(), dto.getShopId());
        List<ShoppingProductVo> shoppingCartVos = new ArrayList<>();
        List<Integer> clearShoppingList = new ArrayList<>();
        long total = 0L;
        for (ShoppingCartInfoDo sc : shoppingCarts) {
            //如果商品标签不存在了
            if (sc.getLabelId() != null && sc.getLabelName() == null) {
                clearShoppingList.add(sc.getId());
                continue;
            }
            //计算价格
            ShoppingProductVo shoppingProductVo = new ShoppingProductVo();
            String name = sc.getProductName();
            long productPrice = sc.getProductPrice() * sc.getNum();
            if (!StringUtils.isEmpty(sc.getLabelName())) {
                name += "(" + sc.getLabelName() + ")";
                productPrice = productPrice + sc.getLabelPrice() * sc.getNum();
            }
            shoppingProductVo.setId(sc.getId());
            shoppingProductVo.setName(name);
            shoppingProductVo.setPrice(sc.getProductPrice());
            shoppingProductVo.setProductId(sc.getProductId());
            shoppingProductVo.setLabelId(sc.getLabelId());
            shoppingProductVo.setIcon(sc.getIcon());
            shoppingProductVo.setNum(sc.getNum());
            shoppingCartVos.add(shoppingProductVo);
            total += productPrice;
        }
        //删除脏数据
        for (Integer i : clearShoppingList) {
            shoppingCartMapper.deleteById(i);
        }

        shoppingCartVo.setTotal(total);
        shoppingCartVo.setProducts(shoppingCartVos);
        return Response.success(shoppingCartVo);
    }
}
